<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Provider's registration</title>
<link rel="stylesheet" href="css/main.css" />
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript" src="xui.js"></script>
<script type="text/javascript" src="jquery-1.5.min.js"></script>
<script type="text/javascript" src="jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="templating.js"></script>
</head>
<body>
	<div id="main">
		<p>
			<button id="endTrip" class="buttn">Declared</button>
		</p>
		<p>
			<button id="exit" class="buttn">Exit from system</button>
		</p>
		<p>
			<button id="start" class="buttn">Start trip</button>
		</p>
		<p>
			<button id="end" class="buttn">End trip</button>
		</p>
		<div id="sendData">
			<h2>Information for user</h2>
			<p>
				<label>How much time you need for get to this user: </label></br> <input
					id="time" type="text" value="100" />
			</p>
			<p>
				<label>If you want, write some infomation for user: </label></br> <input
					id="inf" type="text" />
			</p>
			<button id="sendInf" class="buttn">Send information</button>
		</div>
		<div id="userList">
			<p>This users choose you.</p>
		</div>

	</div>
</body>
</html>
<script type="text/javascript" charset="utf-8">
	var lastReceived = 0;
	var curentIP = "10.0.2.2:9000";
	var userId;
	var pLat = 13.12; //default value. Must be overwritten with real latitude
	var pLong = 10.10; //default value. must be overwritten with real longitude
	var userList = document.getElementById("userList");
	var endConnection = false;
	var endTrip = "http://" + curentIP + "/provider/end";
	var exit = "http://" + curentIP + "/provider/exit";
	var waitEvents = "http://" + curentIP + "/provider/waitEvents";
	var provAccept = "http://" + curentIP + "/provider/accept";
	var lockUser = "http://" + curentIP + "/provider/lock";
	var reached = "http://" + curentIP + "/provider/reached";
	var declare = "http://" + curentIP + "/provider/declare"

	document.getElementById('userList').style.display = 'inline';
	$.ajax({
		url : declare,
		data : {
			pLat : pLat,
			pLong : pLong
		},
		success : function(events) {
			manager();
		},
		dataType : 'json'
	});

	function manager() {
		var getEvents = function() {
			$.ajax({
				url : waitEvents,
				data : {
					lastReceived : lastReceived
				},
				success : function(events) {
					$(events).each(function() {
						lastReceived = this.id;
						eventManager(this.data);
					})
					getEvents()
				},
				dataType : 'json'
			});
		}
		getEvents();

		function eventManager(event) {

			if (event.status == "u_requested") {
				userId = event.userId;
				addNewUser(userId);
			}
			;
			if (event.status == "u_confirmed") {
				alert("OK, user wait you");
				document.getElementById("start").style.display = 'inline';

			}
			;
			if (event.status == "u_canceled") {
				alert("Sorry but user canceled request for taxi");
				$('#' + event.userId).remove();
				//$.post(declare,{pLat:pLat,pLong:pLong});
				document.getElementById('userList').style.display = 'inline';
			}
			;
			if (event.status == "p_acceptedCanceled") {
				alert("You don't accept request from user with id"
						+ event.userId);
				delUser(event.userId);
			}
		}
	}

	$('#endTrip').click(function end() {
		$.get(endTrip, {
			userId : userId
		});
		window.location.reload();
	});

	$('#exit').click(function(p) {
		$.ajax({
			url : endTrip,
			data : {
				userId : userId
			},
			success : function(events) {
				$.post(exit);
			},
			dataType : 'json'
		});
		navigator.app.exitApp();
	});

	function selectUser(id) {
		userId = id;
		document.getElementById("sendData").style.display = 'inline';
		document.getElementById("exit").style.display = 'none';
		document.getElementById("userList").style.display = 'none';
	};
	$('#sendInf').click(function() {
		var inf = $('#inf').val();
		var time = $('#time').val();
		document.getElementById("sendData").style.display = 'none';
		$.post(provAccept, {
			inf : inf,
			time : time,
			userId : userId
		});
	});

	function addNewUser(id) {
		var user = document.createElement("div");
		user.className = "users";
		user.setAttribute('id', id);
		user.setAttribute('onClick', "selectUser(this.id);");
		user.innerHTML = "User #" + id;
		userList.appendChild(user);
	}
	$('#start').click(function() {
		$.post(lockUser, {
			userId : userId
		});
		document.getElementById("start").style.display = 'none';
		document.getElementById("end").style.display = 'inline';
	});
	$('#end').click(function() {
		$.post(reached, {
			userId : userId
		});
		document.getElementById("end").style.display = 'none';
		document.getElementById("endTrip").style.display = 'inline';
		document.getElementById("exit").style.display = 'inline';
	});

	function delUser(userId) {
		elem = document.getElementById(userId);
		userList.removeChild(elem);
	}
</script>